package com.example.gpstracking.netandtask;

import java.util.ArrayList;

import org.json.JSONException;

import com.example.gpstracking.database.MySQLiteManager;
import com.example.gpstracking.database.MySqliteHelper;
import com.example.gpstracking.object.Data;

import android.content.Context;
import android.os.AsyncTask;

public class MyTaskflushMail extends AsyncTask<Void, Void, Void>{

	private Context mContext;
	private String mMailBody;
	private MySQLiteManager mMySQLiteManager;
	public MyTaskflushMail(Context context)
	{
		mContext = context;
		
	}
	@Override
	protected Void doInBackground(Void... params) {
		// TODO Auto-generated method stub
		mMySQLiteManager = new MySQLiteManager(mContext);
		ArrayList<Data> datas = mMySQLiteManager.getAllFromTable(MySqliteHelper.NAME_TABLE);
		if(datas != null)
		{
			mMySQLiteManager.clearTable(MySqliteHelper.NAME_TABLE);
			try {
				mMailBody = NetSupport.JsonConvert(datas);
				SendMail("iva.testmail@gmail.com","ivatestmail","iva_location@yahoo.com",mMailBody);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		return null;
	}
	
	public void SendMail(String user, String password,String recipients, String mailBody) 
	{
		GmailSender sender = new GmailSender(user, password);
        try {
			sender.sendMail("Location",   
			        mailBody,   
			        user,   
			        recipients);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			NetSupport.turnOFFDataTrans(mContext);
		}   
	}
	

}
